package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.context.UserContext;
import com.manage.platform.dto.ProfessionalTitleDto;
import com.manage.platform.dto.query.ProfessionalTitlePageQuery;
import com.manage.platform.entity.ProfessionalTitleEntity;
import com.manage.platform.exception.CommonException;
import com.manage.platform.mapper.ProfessionalTitleMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.CommonVo;
import com.manage.platform.vo.ProfessionalTitleVo;
import com.manage.platform.vo.UserVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * YWD
 * 2025/9/19
 */
@AllArgsConstructor
@Service
public class ProfessionalTitleServiceImpl extends ServiceImpl<ProfessionalTitleMapper, ProfessionalTitleEntity> {

    private final UserServiceImpl userServiceImpl;

    public RPage<List<ProfessionalTitleVo>> getProfessionalTitlePage(ProfessionalTitlePageQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<ProfessionalTitleVo> list = this.baseMapper.getProfessionalTitlePage(query);
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public ProfessionalTitleVo getProfessionalTitleDetails(Long id) {
        return this.baseMapper.getProfessionalTitleDetails(id);
    }

    public Boolean saveProfessionalTitle(ProfessionalTitleDto dto) {
        //职称是否重复
        LambdaQueryWrapper<ProfessionalTitleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProfessionalTitleEntity::getIsDeleted, 0)
                .eq(ProfessionalTitleEntity::getName, dto.getName());
        List<ProfessionalTitleEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("该职称已存在");
        }
        UserVo userVo = UserContext.getUser();
        dto.setUserId(userVo.getId());
        return save(BeanUtil.copyProperties(dto, ProfessionalTitleEntity.class));
    }

    public Boolean updateProfessionalTitle(ProfessionalTitleDto dto) {
        //职称是否重复
        LambdaQueryWrapper<ProfessionalTitleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProfessionalTitleEntity::getIsDeleted, 0)
                .eq(ProfessionalTitleEntity::getName, dto.getName())
                .ge(ProfessionalTitleEntity::getId, dto.getId());
        List<ProfessionalTitleEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("该职称已存在");
        }
        return updateById(BeanUtil.copyProperties(dto, ProfessionalTitleEntity.class));
    }

    public Boolean deleteProfessionalTitle(Long id) {
        //是否正在使用
        Boolean b = userServiceImpl.getByProfessionalId(id);
        if (b) {
            throw new CommonException("职称正在使用,删除失败");
        }
        return this.removeById(id);
    }

    public List<CommonVo> getProfessionalList(String name) {
        return this.baseMapper.getProfessionalList(name);
    }
}
